package com.tao.common.creo.domain;


import com.tao.common.creo.enums.ResultCode;
import lombok.Data;

@Data
public class Result<T> {
    private int code;
    private String message;
    private T data;

    public static <T>Result<T> success() {
        return assembleResult(null,ResultCode.SUCCESS);
    }

    public static <T>Result<T> success(T data) {
        return assembleResult(data,ResultCode.SUCCESS);
    }
    public static <T>Result<T> fail() {
        return assembleResult(null,ResultCode.FAILED);
    }

    public static <T>Result<T> fail(T data) {
        return assembleResult(data,ResultCode.FAILED);
    }
    public static <T>Result<T> fail(Integer code, String message) {
        return assembleResult(code, message, null);
    }

    /**
     * 知道错误码
     * @param resultCode 指定错误码
     * @return
     * @param <T>
     */
    public static <T>Result<T> fail(ResultCode resultCode) {
        return assembleResult(null,resultCode);
    }

    private static <T> Result <T> assembleResult(T data, ResultCode resultCode) {
        Result<T> result = new Result<>();
        result.setCode(resultCode.getCode());
        result.setData(data);
        result.setMessage(resultCode.getMessage());
        return result;
    }

    private static <T> Result <T> assembleResult(int code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMessage(msg);
        return result;
    }
}
